package com.example.superoa.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.mappers.StaffMapper;
import com.example.superoa.model.pojos.Contract;
import com.example.superoa.model.pojos.Ruzhi;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.service.HrDepartService;
import com.example.superoa.model.service.StaffService;
import com.example.superoa.model.vo.HrDepartVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
@SessionAttributes({"ruzhi","manager","ifsp","daipositive","positiveshqidate","myname","departureinformation","staffxq"})
public class DepartController {
	@Autowired
	private HrDepartService hds;
	@Autowired
	private StaffMapper sm;

	
	Shijian sj=new Shijian();
	
	
	//部门管理
	@RequestMapping("hr-department")
	public void depart() {
		hds.initDepart();
	}
	//初始化部门值
	@RequestMapping("init-depart")
	@ResponseBody
	public List<HrDepartVo> initdepart() {
		return hds.initDepart();
	}
	//新增子部门
	@RequestMapping("addoreditor-depart")
	@ResponseBody
	public int adddepart(HrDepartVo hr) {
		return hds.addDepart(hr);
	}
	//修改部门信息
	@RequestMapping("update-depart")
	@ResponseBody
	public void updatedepart(int parentsid,int id) {
		 hds.updateDepart(parentsid, id);
	}
	//删除部门
	@RequestMapping("delete-depart")
	@ResponseBody
	public int deletedepart(int departsid) {
		return hds.deleteDepart(departsid);
	}
	
	//职位管理
	@RequestMapping("hr-position")
	public void hrposition() {
		
	}
	//新增职位、部门和职级关系
	@RequestMapping("add-pdr")
	@ResponseBody
	public void pdr(int postion_id,int Depart_id,int rank_id) {
		hds.addPositionDepart(postion_id, Depart_id, rank_id);
	}
	//查询所有职位、部门和职级关系
	@RequestMapping("select-pdr")
	@ResponseBody
	public JSONArray selectpdr() {
		return hds.selectPositionDepart();
	}
	//查询所有职级
	@RequestMapping("select-rank")
	@ResponseBody
	public JSONArray selecrank() {
		return hds.selectrank();
	}
	//删除职位、部门和职级关系
	@RequestMapping("delete-pdr")
	@ResponseBody
	public int deletepdr(int postion_id,int Depart_id,int rank_id) {
		return hds.deletepdr(postion_id, Depart_id, rank_id);
	}
	
	//员工入职
	@RequestMapping("hr-induction")
	public void hrinduction() {
		
	}
	//员工详细信息填写1
		@RequestMapping("hr-information")
		public void hrinformation() {
			
		}
		//员工详细信息填写2
		@RequestMapping("hr-informationxz")
		public void hrinformationxz() {
					
		}
		
		//职位调动
		@RequestMapping("hr-mobilize")
		public void hrmobilize() {
			
		}
		
		//待入职
		@RequestMapping("hr-dairuzhi")
		@ResponseBody
		public JSONArray dairuzhi(Model model) {
			model.addAttribute("ifsp", 0);//是否已发送入职审批，0代表没有，1代表有
			return hds.selectDairuzhi();
		}
		//
		@RequestMapping("hr-informationsure")
		public void hrinformationsure() {
		}

		
		//入职提交
		@RequestMapping("hr-ruzhicommit")
		@ResponseBody
		public void ruzhitijiao(int shqiren,int shpiren,int id,Date birthday, Date dateentry, String education, String idaddress, String localaddress,
				String marriage, String national, String qq, String name, String phone, String political, String sex,
				String tupian, int staffType, String urgent, String urgentPhone, String weixin,
				int zhengjian, String zhengjianhao, Date ht_start, Date ht_end, Date ht_finish,int Depart_id,
				int position_id, int ht_type,Model model) {
			
			hds.ruzhi(shqiren,shpiren,id,birthday, dateentry, education, idaddress, localaddress, marriage, national, qq, name, phone, political, sex, tupian, staffType, urgent, urgentPhone, weixin, zhengjian, zhengjianhao, ht_start, ht_end, ht_finish, Depart_id, position_id, ht_type);
			model.addAttribute("ifsp", 1);//是否已发送入职审批，0代表没有，1代表有
			System.err.println("入职提交："+name);
			}
		
		//发起入职审批之前进
		@RequestMapping("ruzhishpi")
		@ResponseBody
		public void ruzhishpi(int id,int Depart_id,Model model) {
			//根据待入职的简历id查询该条信息
			model.addAttribute("ruzhi", hds.selectDairuzhishpi(id));
			model.addAttribute("manager", hds.selectStaffByDepartname(2));
		}
		
		@RequestMapping("zaizhistaff")
		@ResponseBody
		public JSONArray selectAllStaffs() {
			return hds.selectAllStaffs();
		}
		//放弃入职，修改入职表是否入职状态
		@RequestMapping("giveupruzhi")
		@ResponseBody
		public void giveupruzhi(int id) {
			hds.updateRuzhiById(id);
		}
		//入职审批
		@RequestMapping("ruzhi-approval")
		@ResponseBody
		public JSONArray ruzhiapproval(HttpSession session) {
			int  staff_id=Integer.valueOf(session.getAttribute("sid").toString());
			System.err.println("员工id："+staff_id);
			return hds.selectShpiBystaffid(staff_id);
		}
		
		//
		@RequestMapping("ruzhishpitwo")
		@ResponseBody
		public void ruzhishpitwo(int id,int Depart_id, Model model) {
			//根据待入职的简历id查询该条信息
			model.addAttribute("ruzhi", hds.selectShpiByid(id));
			model.addAttribute("manager", hds.selectStaffByDepartname(2));
		}
		
		//查询放弃入职名单
		@RequestMapping("selectRuzhiBysfzz")
		@ResponseBody
		public JSONArray selectRuzhiBysfzz() {
			return hds.selectRuzhiBysfzz();
		}
		
		//查询入职审核记录
		@RequestMapping("selectShpiAll")
		@ResponseBody
		public JSONArray selectShpiAll(Model model) {
			model.addAttribute("ruzhi", hds.selectShpiAll());
			return hds.selectShpiAll();
		}
		
		//审批确认入职
		@RequestMapping("sure-ruzhi")
		@ResponseBody
		public void sureruzhi(int id) {
			hds.sureruzhi(id);
		}
		//驳回入职审批
		@RequestMapping("false-ruzhi")
		@ResponseBody
		public void falseruzhi(int id,String approval_reason) {
				hds.falseruzhi(id,approval_reason);
		}
		//查询所有待转正
		@RequestMapping("selectpositive")
		@ResponseBody
		public JSONArray selectPositive() {
				return hds.selectPositive();
		}
		//发起待转正审批
		@RequestMapping("selectpositivebystaffid")
		@ResponseBody
		public JSONArray selectPositiveBystaffid(int staff_id,int Depart_id,Model model) {
			Date date=new Date(sj.getNetworkTime());
			String systemdate=(date.getYear()+1900)+"-"+(date.getMonth()+1)+"-"+date.getDate();	
			model.addAttribute("positiveshqidate", systemdate);
			model.addAttribute("daipositive",  hds.selectPositiveBystaffid(staff_id));
			model.addAttribute("manager", hds.selectStaffByDepartname(2));
			return hds.selectPositiveBystaffid(staff_id);
		}
		//提交待转正审批
		@RequestMapping("positiveapproval")
		@ResponseBody
		public void positiveapproval(Date BecomeDate,Date salarydate,int shqiren,int shpiren,
				int Bwr_id,String reasondd) {
					hds.positiveapproval(BecomeDate, salarydate, shqiren, shpiren, Bwr_id, reasondd);
		}
		//显示需要审批的内容
		@RequestMapping("zhuanzheng-approval")
		@ResponseBody
		public JSONArray zhuanzhengapproval(HttpSession session) {
			int  staff_id=Integer.valueOf(session.getAttribute("sid").toString());
			System.err.println("员工id："+staff_id);
			return hds.daizhuanzheng(staff_id);
		}
		//审批前需要查询的
		@RequestMapping("zhuanzhengshpi")
		@ResponseBody
		public JSONArray zhuanzhengshpi(int staff_id,int Depart_id,Model model) {
			System.err.println("传来了："+staff_id);
			//根据员工id查询该条转正表信息
			Date date=new Date(sj.getNetworkTime());
			String systemdate=(date.getYear()+1900)+"-"+(date.getMonth()+1)+"-"+date.getDate();	
			model.addAttribute("positiveshqidate", systemdate);
			model.addAttribute("daipositive",  hds.beforepositiveshpi(staff_id));
			model.addAttribute("manager", hds.selectStaffByDepartname(2));
			return hds.beforepositiveshpi(staff_id);
		}
		
		//转正审批
		@RequestMapping("hr-positiveinformation")
		public void hrpositiveinformation() {
		}
		//确认转正
		@RequestMapping("surepositive")
		@ResponseBody
		public void surepositive(int Bwr_id) {
			hds.surepositive(Bwr_id);
		}
		//驳回转正审批
		@RequestMapping("false-zhuanzheng")
		@ResponseBody
		public void falsezhuanzheng(int Bwr_id,String approval_reason) {
				hds.falsezhuanzheng(Bwr_id,approval_reason);
		}
		//转正审批的所有结果
		@RequestMapping("positivestate")
		@ResponseBody
		public JSONArray positivestate() {
			return hds.positivestate();
		}
		//已转正结果
		@RequestMapping("positivestated")
		@ResponseBody
		public JSONArray positivestated() {
			return hds.positivestated();
			}
		//离职界面
		@RequestMapping("hr-departure")
		public void hrdepartrue(HttpSession session,Model model){
			//当前员工id
			int  staff_id=Integer.valueOf(session.getAttribute("sid").toString());
			
			System.err.println(sm.staffById(staff_id).getStaff_name());
			model.addAttribute("myname", sm.staffById(staff_id).getStaff_name());
			model.addAttribute("manager", hds.selectStaffByDepartname(2));
			
			Date date=new Date(sj.getNetworkTime());
			String systemdate=(date.getYear()+1900)+"-"+(date.getMonth()+1)+"-"+date.getDate();	
			model.addAttribute("positiveshqidate", systemdate);
		}
		
		
		//离职提交
		@RequestMapping("lizhicommit")
		@ResponseBody
		public void lizhicommit(Date shqiriqi,String lizhireason,String lizhiquxiang,
				Date salarydate,Date endworkdate,int shpiren,HttpSession session) {
			int  staff_id=Integer.valueOf(session.getAttribute("sid").toString());//当前用户id
				hds.lizhigameover(shqiriqi, lizhireason, lizhiquxiang, salarydate, endworkdate, shpiren, staff_id);
			}
		
		
		//通过离职审批（审批人）
		@RequestMapping("updatedeparture")
		@ResponseBody
		public void updatedeparture(int rank_id,String rank_bohui,int pd,HttpSession session) {
			Date date=new Date(sj.getNetworkTime());
			hds.passdeparture(rank_id, date, rank_bohui, pd);
		}
		//查询离职审批
		@RequestMapping("selectdeparture")
		@ResponseBody
		public Map<String,Object> ranksheAll(int page,int rows,HttpSession session){
		int  shpiren=Integer.valueOf(session.getAttribute("sid").toString());//当前用户id
		Map<String,Object> map = new HashMap<>();
		//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray i=hds.selectdepartureByshpi(shpiren);
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", i);
		return map;
	}
		//离职审批界面
		@RequestMapping("hr-departureapproval")
		public void hrdepartureapproval() {
			
		}
		//离职审批
	@RequestMapping("approvaldeparture")
	@ResponseBody
	public JSONArray approvaldeparture(int rank_id,int Depart_id,Model model) {
		model.addAttribute("departureinformation", hds.selectdepartureByid(rank_id));
		model.addAttribute("manager", hds.selectStaffByDepartname(2));
		return hds.selectdepartureByid(rank_id);
	}
	
	//查询离职审批结果
	@RequestMapping("selectdepartureresult")
	@ResponseBody
	public Map<String,Object> rankAllresult(int page,int rows,HttpSession session){
	int  shpiren=Integer.valueOf(session.getAttribute("sid").toString());//当前用户id
	Date date=new Date(sj.getNetworkTime());
	Map<String,Object> map = new HashMap<>();
	//先分页
	Page<Object> pg = PageHelper.startPage(page, rows);
	JSONArray i=hds.selectdepartureResult(shpiren);
	for (Object object : i) {
		JSONObject o=(JSONObject)object;
		if(sj.getPanduanShi2(date, o.getDate("endworkdate"))==0&&o.getString("Staff_sfzz").equals("是")&&o.getString("rank_shpiresult").equals("已通过")) {
			o.put("panduan", 0);
		}else if(sj.getPanduanShi2(date, o.getDate("endworkdate"))==-1&&o.getString("Staff_sfzz").equals("是")&&o.getString("rank_shpiresult").equals("已通过")){
			o.put("panduan", 0);
		}else {
			o.put("panduan", 1);
		}
	}
	//总记录
	map.put("total", pg.getTotal());
	//页内容
	map.put("rows", i);
	return map;
}
	//离职(收回权限)
	@RequestMapping("updateLizhi")
	@ResponseBody
	public void updateLizhi(int staff_id) {
		hds.updateLizhi(staff_id);
	}
	//员工信息修改界面
	@RequestMapping("hr-xq")
	public void hrxq() {
		
	}
	//根据staff_id查询员工信息
	@RequestMapping("selecthrstaff")
	@ResponseBody
	public void selecthrstaff(int staff_id,Model model) {
		model.addAttribute("staffxq", hds.selecthrstaff(staff_id));
	}
	
}
